package com.duobaoyu.dby.supply.admin.controller.config;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.channel.adapter.enums.ChannelCodeEnum;
import com.duobaoyu.dby.channel.adapter.enums.OperationTypeEnum;
import com.duobaoyu.dby.common.core.constant.StringPool;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.common.core.validator.groups.Add;
import com.duobaoyu.dby.common.core.validator.groups.Edit;
import com.duobaoyu.dby.common.security.annotation.Inner;
import com.duobaoyu.dby.supply.admin.annotation.SupplySysLog;
import com.duobaoyu.dby.supply.admin.converter.SupplyChainAdminConverter;
import com.duobaoyu.dby.supply.biz.service.config.ChannelCallbackMappingService;
import com.duobaoyu.dby.supply.common.biz.dto.config.ChannelCallbackMappingDTO;
import com.duobaoyu.dby.supply.common.biz.dto.config.ChannelCallbackMappingQueryDTO;
import com.duobaoyu.dby.supply.common.biz.vo.config.ChannelCallbackMappingVO;
import com.duobaoyu.dby.supply.db.entity.config.ChannelCallbackMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Objects;

/**
 * 渠道回调管理
 * @author liyang
 * @version 1.0.0
 * @ClassName ChannelCallbackMappingController
 * @createTime 13:38:00 2023/04/21
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/channel-callback")
@Api(value = "channel-callback-mapping", tags = "渠道回调回调路径映射")
public class ChannelCallbackMappingController {

	private final SupplyChainAdminConverter supplyChainAdminConverter;
	private final ChannelCallbackMappingService channelCallbackMappingService;

    /**
     * 提供网关查询接口路径映射
     * @param host
     * @param channelPath
     * @return
     */
    @Inner
	@GetMapping("/mapping/query")
    @ApiOperation(value = "提供网关查询接口路径映射", notes = "提供网关查询接口路径映射")
    public R<String> queryChannelPathMapping (@NotBlank(message = "主机地址不能为空") @RequestParam("host") String host,
                                              @NotBlank(message = "请求路径不能为空") @RequestParam("channelPath") String channelPath) {
        String newPath = channelCallbackMappingService.queryChannelPathMapping(host, channelPath);
        return R.ok(newPath);
    }

	/**
	 * 添加渠道回调路径映射
	 *
	 * @param mappingDTO 入参
	 * @return R<Boolean>
	 */
	@PostMapping("/create")
	@ApiOperation(value = "添加渠道回调路径映射", notes = "添加渠道回调路径映射")
	@SupplySysLog(value = "添加渠道回调路径映射",operationType = OperationTypeEnum.INTERT)
	public R<Boolean> createChannelPathMapping(@Validated(Add.class) @RequestBody ChannelCallbackMappingDTO mappingDTO){
		boolean result = channelCallbackMappingService.createChannelPathMapping(mappingDTO);
		return R.ok(result);
	}

	/**
	 * 修改渠道回调路径映射
	 *
	 * @param mappingDTO 入参
	 * @return R<Boolean>
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改渠道回调路径映射", notes = "修改渠道回调路径映射")
	@SupplySysLog(value = "修改渠道回调路径映射",operationType = OperationTypeEnum.UPDATE)
	public R<Boolean> updateChannelPathMapping(@Validated(Edit.class) @RequestBody ChannelCallbackMappingDTO mappingDTO){
		boolean result = channelCallbackMappingService.updateChannelPathMapping(mappingDTO);
		return R.ok(result);
	}

	/**
	 * 删除渠道回调路径映射
	 *
	 * @return R<Boolean>
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除渠道回调路径映射", notes = "删除渠道回调路径映射")
	@SupplySysLog(value = "删除状态映射信息",operationType = OperationTypeEnum.DELETE)
	public R<Boolean> deleteChannelPathMapping(@NotEmpty(message = "主键Id不能为空") @RequestBody List<Long> channelCallbackMappingIds) {
		boolean result = channelCallbackMappingService.deleteChannelPathMapping(channelCallbackMappingIds);
		return R.ok(result);
	}

	/**
	 * 分页查询渠道回调路径映射信息
	 *
	 * @param mappingDTO 入参
	 * @return R<Page<ChannelCallbackMappingVO>>
	 */
	@GetMapping("/page")
	@ApiOperation(value = "分页查询渠道回调路径映射信息", notes = "分页查询渠道回调路径映射信息")
	@SupplySysLog(value = "分页查询渠道回调路径映射信息",operationType = OperationTypeEnum.SELECT)
	public R<Page<ChannelCallbackMappingVO>> pageChannelCallbackMapping(ChannelCallbackMappingQueryDTO mappingDTO){
		Page<ChannelCallbackMapping> page = channelCallbackMappingService.pageChannelCallbackMapping(mappingDTO);
		Page<ChannelCallbackMappingVO> resultPage = supplyChainAdminConverter.toChannelCallbackMappingPage(page);
		if(Objects.nonNull(resultPage) && CollectionUtils.isNotEmpty(resultPage.getRecords())){
			resultPage.getRecords().forEach(mappingVO -> {
				ChannelCodeEnum codeEnum = ChannelCodeEnum.getChannelCodeEnumByCode(mappingVO.getChannelCode());
				mappingVO.setChannelName(Objects.isNull(codeEnum) ? StringPool.EMPTY : codeEnum.getDescribe());
			});
		}
		return R.ok(resultPage);
	}

}
